package org.nch.videoLauncher.repository;

import org.hibernate.Session;
import org.nch.videoLauncher.domain.VideoItem;
import org.nch.videoLauncher.domain.Profile;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: bill
 * Date: Dec 13, 2008
 * Time: 1:56:14 PM
 * To change this template use File | Settings | File Templates.
 */
public class ProfileRepository {

    public ProfileRepository(){
    }

    public Profile getById(Integer id){

        Session session = HibernateUtil.getSession();
        Profile profile;

        try {
            profile = (Profile) session.load(Profile.class, id);
        }
        finally {
            session.close();
        }

        return profile;
    }

    public List<Profile> getAll(){

        Session session = HibernateUtil.getSession();
        List<Profile> profiles;

        try {
            profiles = session.createQuery("from Profile").list();
        }
        finally {
            session.close();
        }

        return profiles;
    }

    public Profile persist(Profile pf){

        Session session = HibernateUtil.getSession();

        try {
            session.beginTransaction();
            session.saveOrUpdate(pf);
            session.getTransaction().commit();
        }
        finally {
            session.close();
        }

        return pf;
    }

    public List<Profile> persistAll(List<Profile> Profiles){

        Session session = HibernateUtil.getSession();

        try {
            session.beginTransaction();
            for  (Profile pf : Profiles ){
                session.saveOrUpdate(pf);
            }
            session.getTransaction().commit();
        }
        finally {
            session.close();
        }

        return Profiles;
    }

    public void delete(Profile pf){

        Session session = HibernateUtil.getSession();

        try {
            session.beginTransaction();
            session.delete(pf);
            session.getTransaction().commit();
        }
        finally {
            session.close();
        }

    }


}
